﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using edowork.com.SQL2005;

namespace WebInvoicingsys.system.storage
{
    public partial class productsearch_list : System.Web.UI.Page
    {
        //前台输出提示信息变量
        public string alert_msg = "";

        //传入的参数
        string productcode, productname;

        /// <summary>
        /// 取得传入的参数
        /// </summary>
        /// <returns></returns>
        private bool GetParma()
        {
            try
            {
                productcode = Request.Params["productcode"].ToString();
                productname = Request.Params["productname"].ToString();
            }
            catch
            {
                //显示出错信息
                alert_msg = String.Format(Scripts.alert_warning, Errormsg.er_Parampost);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return false;
            }

            return true;
        }


        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns></returns>
        private DataSet GetData()
        {
            //取得传入的参数
            if (GetParma() == false)
            {
                return null;
            }

            string error_msg = "";

            //构造SQL语句
            string sql = @"select a.warehouseid, b.warehousename, a.areaid, b.areaname, a.storageid, b.storagename, 
                                  c.productcode, c.productname, c.productshortname, c.goodsclassname, c.goodsunitname,
                                  c.goodsspecname, c.stackname, c.guardname,
                                  sum(a.qty1) qty1
                              from inventory a, v_storage b, v_product c
                               where a.warehouseid=b.warehouseid
                                 and a.areaid=b.areaid
                                 and a.storageid=b.storageid
                                 and a.productcode=c.productcode";

            sql = String.Format(sql, Session["userid"].ToString());

            sql += (productcode == "") ? "" : String.Format(" and c.productcode like '{0}%'", productcode);
            sql += (productname == "") ? "" : String.Format(" and c.productname like '{0}%'", productname);

            sql += @"  group by a.warehouseid, b.warehousename, a.areaid, b.areaname, a.storageid, b.storagename, 
                                c.productcode, c.productname, c.productshortname, c.goodsclassname, c.goodsunitname,
                                c.goodsspecname, c.stackname, c.guardname";


            sql = String.Format(sql, Session["userid"].ToString());

            Execute exec = new Execute(Session["connectionString"].ToString());

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref error_msg);

            if (ds == null)
            {
                //显示出错信息
                alert_msg = String.Format(Scripts.alert_warning, error_msg);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return null;
            }

            DataSet dsResult = new DataSet();

            DataTable dt = new DataTable("List");

            dt.Columns.Add("dgvID");
            dt.Columns.Add("warehouseid");
            dt.Columns.Add("warehousename");
            dt.Columns.Add("areaid");
            dt.Columns.Add("areaname");
            dt.Columns.Add("storageid");
            dt.Columns.Add("storagename");
            dt.Columns.Add("productcode");
            dt.Columns.Add("productname");
            dt.Columns.Add("goodsclassname");
            dt.Columns.Add("goodsunitname");
            dt.Columns.Add("goodsspecname");
            dt.Columns.Add("stackname");
            dt.Columns.Add("guardname");
            dt.Columns.Add("qty1");


            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                DataRow drnew = dt.NewRow();

                drnew["dgvID"] = (dt.Rows.Count + 1).ToString();
                drnew["warehouseid"] = dr["warehouseid"].ToString();
                drnew["warehousename"] = dr["warehousename"].ToString();
                drnew["areaid"] = dr["areaid"].ToString();
                drnew["areaname"] = dr["areaname"].ToString();
                drnew["storageid"] = dr["storageid"].ToString();
                drnew["storagename"] = dr["storagename"].ToString();
                drnew["productcode"] = dr["productcode"].ToString();
                drnew["productname"] = dr["productname"].ToString();
                drnew["goodsclassname"] = dr["goodsclassname"].ToString();
                drnew["goodsunitname"] = dr["goodsunitname"].ToString();
                drnew["goodsspecname"] = dr["goodsspecname"].ToString();
                drnew["stackname"] = dr["stackname"].ToString();
                drnew["guardname"] = dr["guardname"].ToString();
                drnew["qty1"] = dr["qty1"].ToString();

                dt.Rows.Add(drnew);
            }

            dsResult.Tables.Add(dt);

            return dsResult;
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void bind()
        {
            DataSet ds = GetData();

            if (ds == null)
            {
                return;
            }

            rplist.DataSource = ds.Tables[0];
            rplist.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //查询
                bind();
            }
        }

        //返回
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            string url = "/system/storage/productsearch.aspx";

            Response.Redirect(url);
        }

        //导出
        protected void btnExport_Click(object sender, EventArgs e)
        {
            //取得数据
            DataSet ds = GetData();

            if (ds == null)
            {
                return;
            }

            //构造导出表头
            String[] headname = { "序号", "仓库编码", "仓库名称", "库区编码", "库区名称", "库位编码", "库位名称", "货品条码", "货品全称", "类别", "单位", "规格", "堆放方式", "防护方式", "数量(合计)" };

            //导出
            ExportToFile.ExportToExcel(ds.Tables[0], HttpContext.Current.Response, headname);
        }
    }
}